#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname tools $PACKAGE) || exit 1;
PKG_VERSION=$(pkgversion tools $PACKAGE) || exit 2;
PKG_ARCHIVE=$(pkgarchive tools $PACKAGE) || exit 3;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
./unpack.sh $PKG_ARCHIVE || exit 4;
cd ../$PKG_ARCHIVE || (errcho "Can not change into directory ../$PKG_ARCHIVE" && exit 5);
#######################################
# prepare package source (patch etc.)
tar -xf ../mpfr-3.1.2.tar.xz && \
mv -v mpfr-3.1.2 mpfr && \
tar -xf ../gmp-6.0.0a.tar.xz && \
mv -v gmp-6.0.0 gmp && \
tar -xf ../mpc-1.0.2.tar.gz && \
mv -v mpc-1.0.2 mpc && \
for file in $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h); do
  cp -uv $file{,.orig} && \
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file && \
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file && \
  touch $file.orig;
  if (($? != 0)); then
	errcho "Error while preparing gcc source folder";
	exit 6;
  fi
done
sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure && \
sed -i 's/if \((code.*))\)/if (\1 \&\& \!DEBUG_INSN_P (insn))/' gcc/sched-deps.c && \
rm -Rfv ../gcc-build && \
mkdir -v ../gcc-build && \
cd ../gcc-build


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
../gcc-4.9.1/configure                               \
    --target=$LFS_TGT                                \
    --prefix=/tools                                  \
    --with-sysroot=$LFS                              \
    --with-newlib                                    \
    --without-headers                                \
    --with-local-prefix=/tools                       \
    --with-native-system-header-dir=/tools/include   \
    --disable-nls                                    \
    --disable-shared                                 \
    --disable-multilib                               \
    --disable-decimal-float                          \
    --disable-threads                                \
    --disable-libatomic                              \
    --disable-libgomp                                \
    --disable-libitm                                 \
    --disable-libquadmath                            \
    --disable-libsanitizer                           \
    --disable-libssp                                 \
    --disable-libvtv                                 \
    --disable-libcilkrts                             \
    --disable-libstdc++-v3                           \
    --enable-languages=c,c++    \
    && \
    make && \
    make install



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
cd .. && rm -Rfv gcc-build




#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
cd $LFS/sources && \
rm -Rfv $PKG_ARCHIVE || (errcho "Can not remove directory $PKG_ARCHIVE" && exit 8);
echo "Installed $PACKAGE"
echo " --OK--"
